﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;



public static class EnumTools
{
    // 获得枚举的描述列表

    public static List<string> GetEnumDescriptionList(Type value)
    {
        var strList = new List<string>();
        var memberInfo = value.GetFields();
        foreach (var m in memberInfo)
        {
            if (m.FieldType.Name == value.Name)
            {
                var hds = m.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (hds == null || hds.Length != 1)
                    strList.Add(m.Name);
                else
                    strList.Add((hds[0] as DescriptionAttribute).Description);
            }

        }
        return strList;
    }

    // 获得一个枚举所有说明的完整字符串，用来显示帮助
    public static string GetEnumDescriptionString(Type value)
    {
        string strList = "说明:\n";
        var memberInfo = value.GetFields();
        foreach (var m in memberInfo)
        {
            if (m.FieldType.Name == value.Name)
            {
                var hds = m.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (hds == null || hds.Length != 1)
                    strList = strList + (int)m.GetValue(m) + "、" + m.Name + "\n";
                else
                    strList = strList + (int)m.GetValue(m) + "、" + (hds[0] as DescriptionAttribute).Description + "\n";
            }
        }
        return strList;
    }


    // 获得枚举值是再枚举里的第几项

    public static int GetIndexByEnum<T>(Enum value)
    {
        var list = GetEnumDescriptionList(typeof(T));
        var des = GetDescription(value);
        for (int i = 0; i < list.Count; i++)
        {
            if (string.Equals(list[i], des))
            {
                return i;
            }
        }
        return 0;
    }

    // 获得指定第几项的枚举描述
    public static T GetEnumByDesIndex<T>(int index)
    {
        var strList = GetEnumDescriptionList(typeof(T));
        string des = strList[index];
        return GetEnumByDes<T>(des);
    }

    // 根据描述获得第几个枚举值
    public static T GetEnumByDes<T>(string des)
    {
        var memberInfo = typeof(T).GetFields();
        string name = "";
        foreach (var m in memberInfo)
        {
            if (m.FieldType.Name == typeof(T).Name)
            {
                var hds = m.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (hds == null || hds.Length != 1)
                {
                    name = (m.Name);
                }
                else
                {
                    name = (hds[0] as DescriptionAttribute).Description;
                }
                if (string.Equals(name, des))
                {
                    return (T)m.GetValue(m);
                }
            }

        }
        return default(T);
    }

    //获得枚举值的描述, nameInstead: 如果没有，就返回枚举的名字
    public static string GetDescription(this Enum value, bool nameInstead = true)
    {
        Type type = value.GetType();
        string name = Enum.GetName(type, value);
        if (name == null)
        {
            return null;
        }

        FieldInfo field = type.GetField(name);
        DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;

        if (attribute == null && nameInstead == true)
        {
            return name;
        }
        return attribute == null ? null : attribute.Description;
    }


    // 枚举转为int值
    public static int EnumValueToInt(ValueType value)
    {
        return (int)value;
    }
}
